<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Route_Expense_Edit2 extends Page
{
       public $cmbVendor = null;
       public $lblVendor = null;
       public $cmbAcctType = null;
       public $Label2 = null;
       public $lblType = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript1 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript6 = null;
       public $JTJavaScript5 = null;
       public $JTJavaScript4 = null;
       public $JSValidate = null;
       public $hfError = null;
       public $lblamt = null;
       public $Label12 = null;
       public $btnShowAccount = null;
       public $btnShowType = null;
       public $Label1 = null;
       public $dpDate = null;
       public $memoDesc = null;
       public $lblDesc = null;
       public $cmbAccount = null;
       public $lblAccount = null;
       public $cmbType = null;
       public $edAmount = null;
       public $lblAmount = null;
       public $JTDivAccount = null;
       public $JTGroupBox2 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $JTDivType = null;
       public $JTGroupBox1 = null;
       public $btnAddType = null;
       public $btnDelType = null;
       public $edType = null;
       public $listType = null;
       public $imgBack = null;
       public $btnCancel = null;
       public $btnUpdate = null;
       public $compid = null;
       public $hiddenEmp = null;
       public $hiddenRoute = null;
       public $hiddenExpense = null;
       public $JTSiteTheme1 = null;
       public $Image3 = null;
       function edAmountJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //accept only numbers
       var numero = document.getElementById("edAmount").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edAmount").value = numero;
       <?php

       }

       function edAmountJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edAmount','lblamt');
       <?php

       }


       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var compider3 = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,compider3);
       <?php

       }

       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.getElementById('edAccount').value = " "
       <?php

       }

       function btnShowAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edAccount').value = "";
       document.getElementById('JTDivAccount').Show();

       <?php

       }


       function btnDelTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var TypeIndex = document.getElementById('listType').selectedIndex;
       var TypeValue = document.getElementById('listType')[TypeIndex].value;
       var compider3 = document.getElementById('compid').value;
       AjaxType3(TypeValue,compider3);
       <?php

       }

       function btnAddTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addtype = document.getElementById('edType').value;
       var compider = document.getElementById('compid').value;
       AjaxType2(addtype,compider);
       document.getElementById('edType').value = " ";
       <?php

       }

       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edType').value = " ";
       document.getElementById('JTDivType').Show();
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('route_expense_list2.php?emp='.$this->hiddenEmp->getValue().'&route='.$this->hiddenRoute->getValue().'&expense='.$this->hiddenExpense->getValue());
       }

       function btnUpdateClick($sender, $params)
       {
       $this->validateRequired($this->edAmount->Text, 'lblamt');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       /*if($this->chkBusinessExpense->Checked==true)
       {
       $expense = '1';
       }
       else
       {
       $expense = '0';
       } */
       $exp_t = $this->cmbType->getItemIndex();
       $result_exp = mysql_query("select value from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND id='".$exp_t."'");
       $row_exp = mysql_fetch_array($result_exp);
       $exp = $row_exp['value'];

       $acct_t = $this->cmbAcctType->getItemIndex();

       mysql_query("UPDATE rou_expense_tbl SET
       amount = '".$this->edAmount->Text."',
       type = '".$this->cmbType->getItemIndex()."',
       account = '".$this->cmbAccount->getItemIndex()."',
       acct_type = '".$acct_t."',
       description = '".$this->memoDesc->Text."',
       date = '".$this->dpDate->Text."',
       vendor = '".$this->cmbVendor->getItemIndex()."'
       WHERE expense_id='".$this->hiddenExpense->getValue()."'")
       or die ('UPDATE JOB -' . mysql_error());

       if($acct_t=='Expense')
       {
       mysql_query("UPDATE veh_expense_tbl SET
       type = '".$exp."',
       type_id = '".$this->cmbType->getItemIndex()."',
       amount = '".$this->edAmount->Text."',
       date = '".$this->dpDate->Text."',
       account = '".$this->cmbAccount->getItemIndex()."',
       comments = '".$this->memoDesc->Text."',
       vendor = '".$this->cmbVendor->getItemIndex()."' WHERE route_payment_id='".$this->hiddenExpense->getValue()."'")or die('UPDATE VEH EXPENSE-'.mysql_error());
       }

       redirect('route_expense_list2.php?emp='.$this->hiddenEmp->getValue().'&route='.$this->hiddenRoute->getValue().'&expense='.$this->hiddenExpense->getValue());
       }
       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }

       function Route_Expense_Edit2Create($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblamt->Caption = '';

       $this->compid->Value = $_SESSION['company_id'];
       $emp = $_GET['emp'];
       $route = $_GET['route'];
       $expense = $_GET['expense'];
       $this->hiddenEmp->setValue($emp);
       $this->hiddenRoute->setValue($route);
       $this->hiddenExpense->setValue($expense);
       $this->imgBack->setLink('route_expense_list2.php?emp='.$this->hiddenEmp->getValue().'&route='.$this->hiddenRoute->getValue().'&expense='.$this->hiddenExpense->getValue());

       if($_SESSION['access_route']==1)
       {
       $this->btnCancel->Visible=false;
       $this->btnUpdate->Visible=false;
       }

       if($_SESSION['access_route']==2)
       {
       $this->btnCancel->Visible=true;
       $this->btnUpdate->Visible=true;
       }

       $query_expense = "SELECT * FROM rou_expense_tbl WHERE expense_id='".$this->hiddenExpense->getValue()."'";
       $result_expense = mysql_query($query_expense)or die(mysql_error());
       $row_expense = mysql_fetch_array($result_expense);

       /*if($row_expense['business_expense']=="1")
       {
       $this->chkBusinessExpense->Checked=true;
       }
       else
       {
       $this->chkBusinessExpense->Checked=false;
       } */

       $this->memoDesc->Text = $row_expense['description'];
       $this->edAmount->Text = $row_expense['amount'];
       $this->cmbAccount->ItemIndex = $row_expense['account'];
       $this->cmbType->ItemIndex = $row_expense['type'];
       $this->dpDate->Text = $row_expense['date'];
       $this->cmbAcctType->ItemIndex = $row_expense['acct_type'];

       $this->cmbAccount->Clear();
       $this->cmbAccount->AddItem('--Select--', null, null);
       $this->cmbAccount->ItemIndex=$row_expense['account'];
       $result_account = mysql_query("SELECT * FROM lookup_expense_tbl WHERE category='accounts' AND company_id='".$_SESSION['company_id']."'");

       while($row_account = mysql_fetch_array($result_account))
       {
       $this->cmbAccount->AddItem($row_account['value'], null, $row_account['id']);
       }

       //list account
       $this->edAccount->Text = "";
       $result_a = mysql_query("SELECT * FROM lookup_expense_tbl WHERE category='accounts' AND company_id='".$_SESSION['company_id']."'");

       while($row_a = mysql_fetch_array($result_a))
       {
       $this->listAccount->AddItem($row_a['value'], null, $row_a['id']);
       }


       $this->cmbVendor->Clear();
       $this->cmbVendor->AddItem('--Select--',null,'--Select--');
       $this->cmbVendor->ItemIndex=$row_expense['vendor'];
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbVendor->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       $this->cmbVendor->AddItem('Others', null, 'Others');



       $this->cmbType->Clear();
       $this->cmbType->AddItem('--Select--', null, null);
       $this->cmbType->ItemIndex=$row_expense['type'];
       $result_type = mysql_query("SELECT * FROM lookup_expense_tbl WHERE category='types' AND company_id='".$_SESSION['company_id']."'");

       while($row_type = mysql_fetch_array($result_type))
       {
       $this->cmbType->AddItem($row_type['value'], null, $row_type['id']);
       }

        //list type
       $this->edType->Text = "";
       $result_t = mysql_query("SELECT * FROM lookup_expense_tbl WHERE category='types' AND company_id='".$_SESSION['company_id']."'");

       while($row_t = mysql_fetch_array($result_t))
       {
       $this->listType->AddItem($row_t['value'], null, $row_t['id']);
       }
       }



       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

}

global $application;

global $Route_Expense_Edit2;

//Creates the form
$Route_Expense_Edit2=new Route_Expense_Edit2($application);

//Read from resource file
$Route_Expense_Edit2->loadResource(__FILE__);

//Shows the form
$Route_Expense_Edit2->show();

?>